package com.example.lx6_4

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.lx6_4.adapter.OrderAdapter
import com.example.lx6_4.databinding.ActivityMainBinding
import com.example.lx6_4.order.OrderViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, OrderViewModel>() {

    val orderAdapter by lazy { OrderAdapter() }
    var type = "网络"
    override fun initView() {
        binding.rv.apply {
            adapter = orderAdapter
            layoutManager = LinearLayoutManager(this@MainActivity)
        }
        lifecycleScope.launch {
            vm.getOrder(type).collect{
                orderAdapter.submitData(it)
            }
        }
        binding.btn.setOnClickListener {
            ARouter.getInstance().build("/app/AddOrderActivity").navigation()
        }

        binding.btn1.setOnClickListener {
            type = "网络"
            lifecycleScope.launch {
                vm.getOrder(type).collect{
                    orderAdapter.submitData(it)
                }
            }
        }

        binding.btn2.setOnClickListener {
            type = "数据库"
            lifecycleScope.launch {
                vm.getOrder(type).collect{
                    orderAdapter.submitData(it)
                }
            }
        }

        ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                vm.getDeleteOrderRoom(orderAdapter.snapshot()[position]!!)
                vm.getDeleteOrder(orderAdapter.snapshot()[position]!!.id)
                orderAdapter.notifyItemRemoved(position)
                lifecycleScope.launch {
                    vm.getOrder(type).collect{
                        orderAdapter.submitData(it)
                    }
                }
                ToastUtils.showLong("删除成功")
            }

        }).attachToRecyclerView(binding.rv)

    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getOrder("网络").collect{
                orderAdapter.submitData(it)
            }
        }
    }
}